package com.rabbitmq.exec.hello;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description
 * @Author lhj
 * @Date 2024/4/13 15:52
 */
@Slf4j
public class Producer {

    public static final String QUEUE_NAME = "hello";
    
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.56.10");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123");
        try (Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel()){
            /**
             * 声名一个队列
             * 1. 队列名称
             * 2. 队列是否持久化 默认是存储在内存中的
             * 3. 该队列是否只供一个消费者消费 是否进行共享 true 可以多个消费者消费
             * 4. 是否自动删除 最后一个消费者端断开连接以后 该队列是否自动删除 true 自动删除
             * 5. 其他参数
             */
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "hello world";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
            log.info("消息发送完毕: {}", message);
        }
    }
    
}
